﻿// This source is subject to Microsoft Public License (Ms-PL).
// Please see http://taskcardcreator.codeplex.com for details.
// All other rights reserved.

using System;
using System.Globalization;
using System.Windows.Data;
using ReportInterface;

namespace MSFforAgileBasic.Converters
{
  internal class DescriptionConverter : IValueConverter
  {
    private const string descriptionKey = "Description";
    private const string descriptionHtmlKey = "Description HTML";

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      try
      {
        var workItem = value as ReportItem;
        if (workItem != null)
        {
          if (workItem.Fields.ContainsKey(descriptionKey))
          {
            return workItem.Fields[descriptionKey] as string;
          }

          if (workItem.Fields.ContainsKey(descriptionHtmlKey))
          {
            var s = workItem.Fields[descriptionHtmlKey] as string;
            return HtmlRemoval.StripTagsRegex(s);
          }
          return string.Empty;
        }
        return "Error: Incorrect type";
      }
      catch (Exception exception)
      {
        return string.Format("Error: {0}", exception.Message);
      }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }
}